package com.erbao.datastructurearithmetic.base;

/**
 * @author CaoBaoYin
 * @since 2021/8/11 22:10
 */
public class Class01 {

    public static void main(String[] args) {
        int a=1,b=a;
        System.out.println(a+" "+b);
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
        System.out.println(a+" "+b);
    }

    /**
     * @param n 整型数
     * @return 二进制1的个数
     */
    private static int bit1Counts (int n){
        int count = 0;
        while (n != 0){
            int mostRightOne = n & ((~n)+1);
            count++;
            n ^= mostRightOne;
        }
        return count;
    }

    /**
     * 打印出现奇数次的两个数
     * @param arr
     */
    private static void oddTimesTwoNum(int[] arr) {
        // 数组为空，不满足条件的数组
        if (arr == null || arr.length < 2) return;

        int arrLength = arr.length;
        // 只有两个数，这两个不能相等
        if (arrLength == 2 && arr[0] != arr[1]) {
            System.out.println(arr[0] + " " + arr[1]);
            return;
        }

        int eor = arr[0];
        for (int i = 1; i < arrLength; i++) {
            eor ^= arr[i];
        }
        // 此时eor = a ^ b;
        int mostRightOne = eor & ((~eor)+1); // 提取eor最右侧的1.

        int eorp = 0;
        for (int i = 0; i < arrLength; i++) {
            if ((arr[i] & mostRightOne) != 0) {
                eorp ^= arr[i];
            }
        }

        System.out.println(eorp + " " + (eorp^eor));
    }

    /**
     * 找出出现奇数次的一个数
     * @param arr 数组
     * @return 返回这个数
     * @throws IllegalAccessException
     */
    private static int oddTimesNum(int[] arr) throws IllegalAccessException {
        if (arr == null || arr.length == 0) throw new IllegalAccessException("数组为空");

        if (arr.length == 1) return arr[0];

        int eor = arr[0];
        for (int i = 1; i < arr.length; i++) {
            eor ^= arr[i];
        }

        return eor;
    }

    private static void swap(int a , int b){
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;
    }

}
